package com.codegenerator.util;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;

/**
 * JSON处理工具类
 */
public class JsonUtils {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    
    /**
     * 从JSON文件读取对象
     */
    public static <T> T fromJsonFile(String filePath, Class<T> clazz) throws IOException {
        return objectMapper.readValue(new File(filePath), clazz);
    }
    
    /**
     * 将对象写入JSON文件
     */
    public static void toJsonFile(String filePath, Object object) throws IOException {
        objectMapper.writeValue(new File(filePath), object);
    }
    
    /**
     * 将对象转为JSON字符串
     */
    public static String toJsonString(Object object) throws IOException {
        return objectMapper.writeValueAsString(object);
    }
    
    /**
     * 从JSON字符串解析对象
     */
    public static <T> T fromJsonString(String json, Class<T> clazz) throws IOException {
        return objectMapper.readValue(json, clazz);
    }
}
